from selenium.webdriver.common.by import By as by
from Page.UserPage.ErpLoginPage import ErpLoginPage
import random
import time

class CreateNewOrder(ErpLoginPage):
    """业务层"""

    # 零售管理
    retail_management = (by.XPATH, '(//div[@class="ant-menu-submenu-title"])[1]')

    # 零售出库
    retail_delivery = (by.XPATH, '//a[@target="零售出库"]')

    # 新增按钮
    new_order = (by.CSS_SELECTOR, '.table-operator .ant-btn-primary')

    # 点击引导关闭
    skip_button = (by.CSS_SELECTOR, '.introjs-skipbutton')

    # 会员卡号点击
    click_vip = (by.CSS_SELECTOR, '#organId')

    # 会员卡号输入框
    input_vip = (by.CSS_SELECTOR, '#organId .ant-select-search__field')

    # 选择商品
    select_goods = (by.XPATH, '//span[@class="ant-input-group ant-input-group-compact"]/button')

    # 选择最新商品
    new_product = (by.CSS_SELECTOR, 'tbody.ant-table-tbody:nth-child(3)>tr.ant-table-row:nth-of-type(1)')
    """这个可能有问题"""

    # 查看刚刚的库存，只获取了文本
    data_num = (by.CSS_SELECTOR, 'tbody.ant-table-tbody:nth-child(3)>tr:nth-of-type(1)>td:nth-of-type(10)')

    # 确定按钮
    determine = (by.XPATH, '(//button[@class="ant-btn ant-btn-primary"])[6]')

    # 保存并审核
    save_check = (by.CSS_SELECTOR, 'div.ant-modal-footer>button:nth-of-type(3)')

    # 关闭按钮
    close_window = (by.XPATH, '(//button[@class="ant-modal-close"])[2]')

    """操作层"""

    def open_new_vip(self):
        """进入新增页面"""
        self.click(self.retail_management)
        time.sleep(1)
        self.click(self.retail_delivery)
        self.click(self.new_order)
        self.click(self.skip_button)
        time.sleep(1)

    def post_order(self):
        """填写信息并返回库存"""
        self.js_click(self.click_vip)  # 不点不让输
        self.send_keys(self.input_vip, "12314")
        time.sleep(1)
        self.js_click(self.select_goods)
        self.js_click(self.new_product)
        text = self.get_element_text(self.data_num)
        num = int(text)
        self.click(self.determine)
        time.sleep(1)
        self.click(self.save_check)
        time.sleep(1)
        return num

    def get_inventory(self):
        self.click(self.new_order)
        self.js_click(self.select_goods)
        text = self.get_element_text(self.data_num)
        time.sleep(1)
        num = int(text)
        return num

    """业务层"""

if __name__ == '__main__':
    cno = CreateNewOrder()

    cno.common_login()
    cno.open_new_vip()
    num1 = cno.post_order()
    num2 = cno.get_inventory()

    assert num1-num2 == 1
